package com.example.user_module.login;

import com.blankj.utilcode.util.LogUtils;
import com.example.common.mvvm.BaseEntity;
import com.example.common.mvvm.model.BaseRepository;
import com.example.network_sdk.RetrofitManager;
import com.example.user_module.dao.UserDataBase;
import com.example.user_module.dao.UserEntity;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class LoginRepository extends BaseRepository {

    Observable<BaseEntity>saveUser(UserEntity entity, UserDataBase db){
        Observable<UserEntity>observable=new Observable<UserEntity>() {
            @Override
            protected void subscribeActual(Observer<? super UserEntity> observer) {
                db.userDao().delUser();
                db.userDao().insertUser(entity);
                UserEntity userEntity = new UserEntity();
                LogUtils.e("asss"+"asdddd");
                userEntity.message="保存成功";
                userEntity.status=200;
                observer.onNext(userEntity);
                observer.onComplete();
            }
        };
        return functionObservable(observable);
    }

    public Observable<BaseEntity>requestInsertUser(String avatar,long expires_time ,
                                                   long followCount, long likeCount,
                                                   String name ,String qqOpenId ,
                                                   long topCommentCount){
        Observable<UserInfoEntity> userEntityObservable = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).requestInsert(avatar, expires_time, followCount, likeCount, name, qqOpenId, topCommentCount);
        return userEntityObservable.map(new Function<UserInfoEntity, BaseEntity>() {
            @Override
            public BaseEntity apply(@NonNull UserInfoEntity userInfoEntity) throws Exception {
                return userInfoEntity;
            }
        });
    }
}
